package logic

import (
	"context"
	"fmt"
	"user_srv/internal/svc"
	"user_srv/models"
	"user_srv/pkg"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type AuthLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAuthLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AuthLogic {
	return &AuthLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AuthLogic) Auth(in *user.AuthRequest) (*user.AuthResponse, error) {
	// todo: add your logic here and delete this line
	var ur models.IdCardAuth
	ur.UserId = int(in.UserId)

	if err := ur.GetAuthByUserId(); err != nil {
		logx.Error("实名验证失败", err)
		return nil, err
	}

	if ur.ID > 0 {
		logx.Error("用户已实名")

	}

	//调用第三方接口校验实名信息

	if !pkg.AuthRealName(in.IdCard, in.RealName) {
		logx.Error("实名失败,调用第三方接口校验实名消息")
		return nil, fmt.Errorf("实名验证失败")
	}
	ur.Realname = in.RealName
	ur.IdCard = in.IdCard

	if err := ur.Create(); err != nil {
		logx.Error("实名失败", err)
		return nil, err
	}
	return &user.AuthResponse{}, nil
}
